package client

import (
	"github.com/mars/internal/admin/core/domain"
)

// BookingView 用于前端展示的预约信息
type BookingView struct {
	ID               string  `json:"_id,omitempty"`
	BookingNo        string  `json:"bookingNo"`
	OpenID           string  `json:"openid"`
	VenueType        string  `json:"venueType"`
	BookingDate      string  `json:"bookingDate"` // 格式化的日期字符串
	TimeSlot         string  `json:"timeSlot"`
	TicketType       string  `json:"ticketType"`
	Status           string  `json:"status"`
	Remark           string  `json:"remark,omitempty"`
	CancelReason     string  `json:"cancelReason,omitempty"`
	RejectReason     string  `json:"rejectReason,omitempty"`
	CancelTime       *string `json:"cancelTime,omitempty"`       // 格式化的时间字符串
	RejectTime       *string `json:"rejectTime,omitempty"`       // 格式化的时间字符串
	CreateTime       string  `json:"createTime"`                 // 格式化的时间字符串
	UpdateTime       string  `json:"updateTime"`                 // 格式化的时间字符串
	VenueName        string  `json:"venueName,omitempty"`        // 场地名称
	VenueDescription string  `json:"venueDescription,omitempty"` // 场地描述
	TimeSlotName     string  `json:"timeSlotName,omitempty"`     // 时间段名称
	UserNickName     string  `json:"userNickName,omitempty"`     // 用户昵称
	UserAvatarUrl    string  `json:"userAvatarUrl,omitempty"`    // 用户头像
}

// ToBookingView 将domain.Booking转换为BookingView
func ToBookingView(booking *domain.Booking) *BookingView {
	if booking == nil {
		return nil
	}

	view := &BookingView{
		ID:               booking.ID,
		BookingNo:        booking.BookingNo,
		OpenID:           booking.OpenID,
		VenueType:        string(booking.VenueType),
		BookingDate:      formatTimestamp(booking.BookingDate, TimeFormatDate),
		TimeSlot:         string(booking.TimeSlot),
		TicketType:       string(booking.TicketType),
		Status:           string(booking.Status),
		Remark:           booking.Remark,
		CancelReason:     booking.CancelReason,
		RejectReason:     booking.RejectReason,
		CancelTime:       formatTimestampPtr(booking.CancelTime, TimeFormatDateTime),
		RejectTime:       formatTimestampPtr(booking.RejectTime, TimeFormatDateTime),
		CreateTime:       formatTimestamp(booking.CreateTime, TimeFormatDateTime),
		UpdateTime:       formatTimestamp(booking.UpdateTime, TimeFormatDateTime),
		VenueName:        booking.VenueName,
		VenueDescription: booking.VenueDescription,
		TimeSlotName:     booking.TimeSlotName,
		UserNickName:     booking.UserNickName,
		UserAvatarUrl:    booking.UserAvatarUrl,
	}

	return view
}

// ToBookingViewList 将domain.Booking列表转换为BookingView列表
func ToBookingViewList(bookings []domain.Booking) []BookingView {
	views := make([]BookingView, len(bookings))
	for i, booking := range bookings {
		if view := ToBookingView(&booking); view != nil {
			views[i] = *view
		}
	}
	return views
}
